﻿
using System;
using System.ServiceModel;
using System.ServiceModel.Channels;

using WSU.Cheka.HelloWCF.Contracts;

namespace WSU.Cheka.HelloWCF.Client
{
    /// <summary>
    /// manually construct the proxy, other than being auto-made by IDE
    /// </summary>
    sealed class HelloWcfClient : ClientBase<IHelloWCF>, IHelloWCF
    {
        #region "constructor"

        /// <summary>
        /// this constructor is for manually setting the endpoint information
        /// </summary>
        public HelloWcfClient(Binding binding, string remoteAddress)
            : base(binding, new EndpointAddress(remoteAddress))
        { }

        /// <summary>
        /// this constructor is for setting the endpoint information by XML configuration file
        /// </summary>
        /// <param name="endpntConfigName">name property of the client element in the XML file</param>
        public HelloWcfClient(string endpntConfigName)
            : base(endpntConfigName)
        { }

        #endregion

        #region "implement IHelloWCF"

        public string Hello(string content)
        {
            return base.Channel.Hello(content);
        }

        public float Add(float x, float y)
        {
            return base.Channel.Add(x, y);
        }

        #endregion
    }

    static class TestHelloWcfClient
    {
        /// <summary>
        /// here we can use "localhost", WCF runtime will take care of the IPV6 and IPV4 issue
        /// </summary>
        private const string SvcAddress = "net.tcp://localhost:9000/HelloWCF";

        private static void CallService(this IHelloWCF proxy)
        {
            Console.WriteLine("Call Hello, return : {0}", proxy.Hello("WSU"));
            Console.WriteLine("Call Add, return : {0}", proxy.Add(12, 45).ToString());
        }

        private static void ManualCallSvcByChannelFactory()
        {
            using (ChannelFactory<IHelloWCF> chFactory = new ChannelFactory<IHelloWCF>(new NetTcpBinding(), SvcAddress))
            {
                IHelloWCF proxy = chFactory.CreateChannel();
                using (proxy as IDisposable)
                {
                    proxy.CallService();
                }
            }
            Console.WriteLine("=========== Manually call service by channel factory successfully.");
        }

        private static void ManualCallSvcByUserdefProxy()
        {
            using (HelloWcfClient proxy = new HelloWcfClient(new NetTcpBinding(), SvcAddress))
            {
                proxy.CallService();
            }
            Console.WriteLine("=========== Manually call service by user-defined client proxy successfully.");
        }

        private static void ConfigCallSvcByChannelFactory()
        {
            // find the proper configuration inforamtion from the XML file by matching the name given to the constructor
            // of ChannelFactory. This name must match one name property of a <client> element in the XML file
            using (ChannelFactory<IHelloWCF> chFactory = new ChannelFactory<IHelloWCF>("HelloWcfClient"))
            {
                IHelloWCF proxy = chFactory.CreateChannel();
                using (proxy as IDisposable)
                {
                    proxy.CallService();
                }
            }
            Console.WriteLine("*********** Config call service by channel factory successfully.");
        }

        private static void ConfigCallSvcByUserdefProxy()
        {
            using (HelloWcfClient proxy = new HelloWcfClient("HelloWcfClient"))
            {
                proxy.CallService();
            }
            Console.WriteLine("*********** Config call service by user-defined client proxy successfully.");
        }

        public static void TestMain()
        {
            ManualCallSvcByChannelFactory();
            ManualCallSvcByUserdefProxy();

            ConfigCallSvcByChannelFactory();
            ConfigCallSvcByUserdefProxy();
        }
    }
}